﻿namespace Hidistro.UI.Common.Controls
{
    using Hidistro.Core;
    using Hidistro.Membership.Context;
    using System;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class BFDScriptLabel : Literal
    {
        private string client;

        protected override void Render(HtmlTextWriter writer)
        {
            SiteSettings siteSettings = HiContext.Current.SiteSettings;
            if ((siteSettings != null) && siteSettings.EnabledBFD)
            {
                this.client = "C" + siteSettings.BFDUserName;
                base.Text = this.ScriptByUrl(HttpContext.Current.Request.Url.ToString().ToLower());
            }
            base.Render(writer);
        }

        protected string ScriptByUrl(string url)
        {
            string str = "<script  type='text/javascript' src='http://res.juseshop.com/utility/BFD.js'></script>";
            string client = this.client;
            string str3 = HiContext.Current.User.UserId.ToString();
            if (url.IndexOf("userdefault") >= 0)
            {
                string str4 = "{user_id :\"" + str3 + "\" , client :\"" + client + "\"  }";
                return (str + "<script type='text/javascript'>BFD(" + str4 + ",'usercenter')</script>");
            }
            if (url.IndexOf("default") >= 0)
            {
                string str5 = "{user_id :\"" + str3 + "\" , client :\"" + client + "\"  }";
                return (str + "<script type='text/javascript'>BFD(" + str5 + ",'default')</script>");
            }
            if (url.IndexOf("unproductdetails") >= 0)
            {
                string str6 = HttpContext.Current.Request.QueryString["productId"];
                StringBuilder builder = new StringBuilder();
                builder.Append("{");
                builder.AppendFormat("id:\"{0}\",", str6);
                builder.AppendFormat("sku_id:{0},", "$(\"#bfdProductCode\").text()");
                builder.AppendFormat("name:{0},", "$(\"#bfdProductTitle\").text()");
                builder.AppendFormat("item_link:{0},", "location.href");
                builder.AppendFormat("image_link:{0},", "location.origin+$(\"#ProductDetails_common_ProductImages___iptPicUrl1\").val()");
                builder.AppendFormat("price:{0},", "changeTwoDecimal($(\"#UnProductDetails_lblBuyPrice\").text())");
                builder.AppendFormat("promotion_price:{0},", "''");
                builder.AppendFormat("categroyDetail:{0},", "GetcategroyDetail(false)");
                builder.AppendFormat("brand:{0},", "$(\"#bfdBrand\").text()");
                builder.AppendFormat("store:{0},", "parseInt($(\"#productDetails_Stock\").text())");
                builder.AppendFormat("del :{0},", "false");
                builder.Append("user_id :\"" + str3 + "\" , client :\"" + client + "\"}");
                return (str + "<script type='text/javascript'>BFD(" + builder.ToString() + ",'productdetail') </script>");
            }
            if (url.IndexOf("productdetails") >= 0)
            {
                string str7 = HttpContext.Current.Request.QueryString["productId"];
                StringBuilder builder2 = new StringBuilder();
                builder2.Append("{");
                builder2.AppendFormat("id:\"{0}\",", str7);
                builder2.AppendFormat("sku_id:{0},", "$(\"#bfdProductCode\").text()");
                builder2.AppendFormat("name:{0},", "$(\"#bfdProductTitle\").text()");
                builder2.AppendFormat("item_link:{0},", "location.href");
                builder2.AppendFormat("image_link:{0},", "location.origin+$(\"#ProductDetails_common_ProductImages___iptPicUrl1\").val()");
                builder2.AppendFormat("price:{0},", "changeTwoDecimal($(\"#ProductDetails_lblBuyPrice\").text())");
                builder2.AppendFormat("promotion_price:{0},", "''");
                builder2.AppendFormat("categroyDetail:{0},", "GetcategroyDetail(false)");
                builder2.AppendFormat("brand:{0},", "$(\"#bfdBrand\").text()");
                builder2.AppendFormat("store:{0},", "parseInt($(\"#productDetails_Stock\").text())");
                builder2.AppendFormat("del :{0},", "true");
                builder2.Append("user_id :\"" + str3 + "\" , client :\"" + client + "\"}");
                return (str + "<script type='text/javascript'>BFD(" + builder2.ToString() + ",'productdetail') </script>");
            }
            if (url.IndexOf("shoppingcart") >= 0)
            {
                StringBuilder builder3 = new StringBuilder();
                builder3.Append("{");
                builder3.AppendFormat("items:{0},", "GetBFDCartitems()");
                builder3.Append("user_id :\"" + str3 + "\" , client :\"" + client + "\"}");
                return (str + "<script type='text/javascript'>BFD(" + builder3.ToString() + ",'cart') </script>");
            }
            if (url.IndexOf("listproduct") >= 0)
            {
                string textToFormat = HttpContext.Current.Request.QueryString["keywords"];
                StringBuilder builder4 = new StringBuilder();
                builder4.Append("{");
                builder4.AppendFormat("search_word:\"{0}\",", DataHelper.CleanSearchString(Globals.UrlDecode(Globals.HtmlEncode(textToFormat))));
                builder4.AppendFormat("result:{0},", "$(\".category_pro_tab\").find(\"li\").length>0?true:false");
                builder4.Append("user_id :\"" + str3 + "\" , client :\"" + client + "\"}");
                return (str + "<script type='text/javascript'>BFD(" + builder4.ToString() + ",'search') </script>");
            }
            if (url.IndexOf("category") >= 0)
            {
                StringBuilder builder5 = new StringBuilder();
                builder5.Append("{");
                builder5.AppendFormat("category_tree:{0},", "GetcategroyDetail(true)");
                builder5.Append("user_id :\"" + str3 + "\" , client :\"" + client + "\"  }");
                return (str + "<script type='text/javascript'>BFD(" + builder5.ToString() + ",'categroy')</script>");
            }
            if (url.IndexOf("paymentreturn_url") >= 0)
            {
                Literal literal = this.Page.Controls[0].FindControl("litMessage") as Literal;
                if (literal.Text.IndexOf("订单已成功完成支付") >= 0)
                {
                    Regex regex = new Regex(@"\d+", RegexOptions.IgnoreCase);
                    string str9 = regex.Match(literal.Text).Value;
                    if (!string.IsNullOrEmpty(str9))
                    {
                        BFDOrder bFDOrder = ControlProvider.Instance().GetBFDOrder(str9);
                        StringBuilder builder6 = new StringBuilder();
                        builder6.Append("{");
                        builder6.AppendFormat("order_id:\"{0}\",", str9);
                        builder6.AppendFormat("order_list:{0},", bFDOrder.orderlist);
                        builder6.AppendFormat("order_sumprice:{0},", bFDOrder.ordertotal);
                        builder6.Append("user_id :\"" + str3 + "\" , client :\"" + client + "\"  }");
                        str = str + "<script type='text/javascript'>BFD(" + builder6.ToString() + ",'buy')</script>";
                    }
                }
                return str;
            }
            if (url.IndexOf("paysucceed") >= 0)
            {
                string str10 = HttpContext.Current.Request.QueryString["orderId"];
                if (!string.IsNullOrEmpty(str10))
                {
                    BFDOrder order2 = ControlProvider.Instance().GetBFDOrder(str10);
                    StringBuilder builder7 = new StringBuilder();
                    builder7.Append("{");
                    builder7.AppendFormat("order_id:\"{0}\",", str10);
                    builder7.AppendFormat("order_list:{0},", order2.orderlist);
                    builder7.AppendFormat("order_sumprice:{0},", order2.ordertotal);
                    builder7.Append("user_id :\"" + str3 + "\" , client :\"" + client + "\"  }");
                    str = str + "<script type='text/javascript'>BFD(" + builder7.ToString() + ",'buy')</script>";
                }
                return str;
            }
            if (url.IndexOf("finishorder.aspx") >= 0)
            {
                string str11 = HttpContext.Current.Request.QueryString["orderId"];
                if (!string.IsNullOrEmpty(str11))
                {
                    BFDOrder order3 = ControlProvider.Instance().GetBFDOrder(str11);
                    StringBuilder builder8 = new StringBuilder();
                    builder8.Append("{");
                    builder8.AppendFormat("order_id:\"{0}\",", str11);
                    builder8.AppendFormat("order_list:{0},", order3.orderlist);
                    builder8.AppendFormat("order_sumprice:{0},", order3.ordertotal);
                    builder8.AppendFormat("order_shippingName:\"{0}\",", order3.ModeName);
                    builder8.AppendFormat("order_payName:\"{0}\",", order3.paymenttype);
                    builder8.Append("user_id :\"" + str3 + "\" , client :\"" + client + "\"  }");
                    str = str + "<script type='text/javascript'>BFD(" + builder8.ToString() + ",'order')</script>";
                }
            }
            return str;
        }
    }
}

